package cn.pet_know.service.impl;

import cn.pet_know.common.constants.DelFlag;
import cn.pet_know.common.my_exception.CustomException;
import cn.pet_know.common.utils.ConverterUtils;
import cn.pet_know.common.utils.GenIdUtil;
import cn.pet_know.dao.mapper.UserMapper;
import cn.pet_know.entity.User;
import cn.pet_know.entity.dto.UserDTO;
import cn.pet_know.entity.vo.UserVO;
import cn.pet_know.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Override
    public Boolean registry(UserDTO userDTO) {
        // 校验参数
        {
            if (Objects.isNull(userDTO.getUserName()) || userDTO.getUserName().length() <= 0) {
                throw new CustomException("userName is null!");
            }
            if (Objects.isNull(userDTO.getUserAccount())) {
                throw new CustomException("userAccount is null!");
            }
            if (Objects.isNull(userDTO.getUserPassword())) {
                throw new CustomException("userAccount is null!");
            }
            if (Objects.isNull(userDTO.getUserAvatar())) {
                throw new CustomException("userAvatar is null!");
            }
            String reg = "^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,6}$";
            if (Objects.isNull(userDTO.getUserEmail()) || !userDTO.getUserEmail().matches(reg)) {
                throw new CustomException("userEmail is null or failed!");
            }
            reg = "^1[3|4|5|7|8][0-9]\\d{8}$";
            if (Objects.isNull(userDTO.getUserPhone()) || !userDTO.getUserPhone().matches(reg)) {
                throw new CustomException("userPhone is null or failed!");
            }
        }
        long uid = GenIdUtil.generatorId();
        userDTO.setUserId(uid);
        // 注册用户
        return this.baseMapper.insert(ConverterUtils.copyR(userDTO, new User())) > 0;
    }

    @Override
    public UserVO findUser(UserDTO userDTO) {
        User user = new User();
        User result = this.baseMapper.selectOne(
                new LambdaQueryWrapper<User>()
                        .setEntity(ConverterUtils.copyR(userDTO, user))
                        .eq(User::getDeleted, DelFlag.EXISTS)
        );
        return ConverterUtils.copyR(result, new UserVO());
    }

    @Override
    public Boolean goOutUser(UserDTO userDTO) {
        if (Objects.isNull(userDTO.getUserId())) {
            throw new CustomException("userId is null !");
        }

        int update = this.baseMapper.update(
                null, new LambdaUpdateWrapper<User>()
                        .set(User::getDeleted, DelFlag.DELETED)
        );
        return update > 0;
    }

    @Override
    public Boolean updateUser(UserDTO userDTO) {
        if (Objects.isNull(userDTO.getUserId())) {
            throw new CustomException("userId is null !");
        }

        return this.update(new LambdaUpdateWrapper<User>()
                .set(Objects.nonNull(userDTO.getUserName()), User::getUserName, userDTO.getUserName())
                .set(Objects.nonNull(userDTO.getUserPassword()), User::getUserPassword, userDTO.getUserPassword())
                .set(Objects.nonNull(userDTO.getUserAvatar()), User::getUserAvatar, userDTO.getUserAvatar())
        );
    }
}